﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using MBSVO;
using MBSBusinessEntities;


namespace MBSBLC
{
    class BLCUtility
    {
        public static Dictionary<string, string> getSeatStatusDictionary(string xmlString)
        {
            Dictionary<string, string> dctSeatAvailability = new Dictionary<string, string>();
            //string xmlString = "<seatlayout><seat status='Available'>A1</seat><seat status='Available'>A2</seat><seat status='Available'>A3</seat><seat status='Available'>A4</seat><seat status='Available'>A5</seat><seat status='Available'>A6</seat><seat status='Available'>A7</seat><seat status='Available'>A8</seat></seatlayout>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new StringReader(xmlString));
            XmlNodeList nodeList = xmlDoc.SelectNodes("/seatLayout/seat");

            foreach (XmlNode node in nodeList)
            {
                string seatName = node.InnerText;
                string status = node.Attributes["status"].Value;
                dctSeatAvailability.Add(seatName, status);
            }
            return dctSeatAvailability;
        }

        public static string setSeatStatusBooked(string xmlString, string seatno)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new StringReader(xmlString));
            XmlNodeList nodeList = xmlDoc.SelectNodes("/seatLayout/seat");

            foreach (XmlNode node in nodeList)
            {
                string seatName = node.InnerText;
                if (seatName.Equals(seatno))
                {
                    node.Attributes["status"].Value = "Booked";
                }
            }

            XmlElement root = xmlDoc.DocumentElement;
            String xml = root.OuterXml;
            return xml;

        }

        public static string setSeatStatusBooked(string xmlString, List<String> seatList)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new StringReader(xmlString));
            foreach (String seat in seatList)
            {
                XmlNodeList nodeList = xmlDoc.SelectNodes("/seatLayout/seat");

                foreach (XmlNode node in nodeList)
                {
                    string seatName = node.InnerText;
                    if (seatName.Equals(seat))
                    {
                        node.Attributes["status"].Value = "Booked";
                    }
                }
                // xmlDoc.Save(xmlString);
                // return xmlDoc.ToString();

            }
            XmlElement root = xmlDoc.DocumentElement;
            String xml = root.OuterXml;
            return xml;
        }


        public static CinemaVO convertCinemaBEtoVO(CinemaBE cinemaBE)
        {
            CinemaVO cinemaVO = new CinemaVO();
            if (cinemaBE != null)
            {
                cinemaVO.CinemaID = cinemaBE.CinemaID;
                cinemaVO.CinemaName = cinemaBE.CinemaName;
                cinemaVO.Location = cinemaBE.Location;
                cinemaVO.NumOfScreens = cinemaBE.NumOfScreens;
                cinemaVO.IsDeleted = cinemaBE.IsDeleted;
            }
            return cinemaVO;
        }

        public static ScreenVO convertScreenBEtoVO(ScreenBE screenBE)
        {
            ScreenVO screenVO = new ScreenVO();
            if (screenBE != null)
            {
                screenVO.CinemaID = screenBE.CinemaID;
                screenVO.ScreenNo = screenBE.ScreenNo;
                screenVO.NumOfSeats = screenBE.NumOfSeats;
                screenVO.IsDeleted = screenBE.IsDeleted;
            }
            return screenVO;
        }

        public static MovieVO convertMovieBEtoVO(MovieBE movieBE)
        {
            MovieVO movieVO = new MovieVO();
            if (movieBE != null)
            {
                movieVO.MovieID = movieBE.MovieID;
                movieVO.MovieTitle = movieBE.MovieTitle;
                movieVO.Description = movieBE.Description;
                movieVO.Duration = movieBE.Duration;
                movieVO.Genre = movieBE.Genre;
                movieVO.Cast = movieBE.Cast;
                movieVO.Rating = movieBE.Rating;
                movieVO.ImageURL = movieBE.ImageURL;
                movieVO.ReleaseDate = movieBE.ReleaseDate;
                movieVO.IsDeleted = movieBE.IsDeleted;
                movieVO.TrailerURL = movieBE.TrailerURL;
            }
            return movieVO;
        }

        public static MovieScheduleVO convertMovieScheduleBEtoVO(MovieScheduleBE movieScheduleBE)
        {
            MovieScheduleVO movieScheduleVO = new MovieScheduleVO();
            if (movieScheduleBE != null)
            {
                movieScheduleVO.CinemaID = movieScheduleBE.CinemaID;
                movieScheduleVO.EndDate = movieScheduleBE.EndDate;
                movieScheduleVO.IsDeleted = movieScheduleBE.IsDeleted;
                movieScheduleVO.MovieID = movieScheduleBE.MovieID;
                movieScheduleVO.MovieScheduleID = movieScheduleBE.MovieScheduleID;
                movieScheduleVO.ScreenNo = movieScheduleBE.ScreenNo;
                movieScheduleVO.ShowStartTime = movieScheduleBE.ShowStartTime;
                movieScheduleVO.ShowEndTime = movieScheduleBE.ShowEndTime; ;
                movieScheduleVO.StartDate = movieScheduleBE.StartDate;
                movieScheduleVO.WeekDateFare = movieScheduleBE.WeekDateFare;
                movieScheduleVO.WeekEndFare = movieScheduleBE.WeekEndFare;
            }
            return movieScheduleVO;
        }

        public static List<MovieScheduleVO> convertMovieScheduleBEListtoVO(List<MovieScheduleBE> movieScheduleBEList)
        {
            List<MovieScheduleVO> movieScheduleVOList = new List<MovieScheduleVO>();
            if (movieScheduleBEList !=null){
                foreach (MovieScheduleBE movieScheduleBE in movieScheduleBEList){
                    MovieScheduleVO movieScheduleVO = convertMovieScheduleBEtoVO(movieScheduleBE);
                    if (movieScheduleVO != null) movieScheduleVOList.Add(movieScheduleVO);
                }
                
            }
            return movieScheduleVOList;
        }


        public static ShowTimeVO convertShowTimeBEtoVO(ShowTimeBE showTimeBE)
        {
            ShowTimeVO showTimeVO = new ShowTimeVO();
            if (showTimeBE != null)
            {
                showTimeVO.ShowID = showTimeBE.ShowID;
                showTimeVO.Fare = showTimeBE.Fare;
                showTimeVO.ShowDateTime = showTimeBE.ShowDateTime;
                showTimeVO.SeatsAvailable = showTimeBE.SeatsAvailable;
                showTimeVO.MovieScheduleID = showTimeBE.MovieScheduleID;
                showTimeVO.IsDeleted = showTimeBE.IsDeleted;
            }
            return showTimeVO;
        }
    }
}
